import { BaseEnvMirror } from "@/modules/dev-env/mirror/mirror.ts";
import type { ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import type { DefaultOptionType } from "ant-design-vue/es/vc-select/Select";
import { MirrorQueryCondition } from "@/modules/dev-env/mirror/condition.ts";
import {
  getLibericaArch,
  getLibericaBundleTypeLabel,
  getLibericaBundleTypes,
  getLibericaJavaVersions,
  getLibericaOsName,
  type LibericaJdkVersionInfo,
} from "@/modules/dev-env/java/jdk/jdk-api.ts";
import { getBitness } from "@/util/system-utils2.ts";

class LibericaJavaVersionCondition extends MirrorQueryCondition<number> {
  constructor() {
    super("Java版本", "version-feature");
  }

  async getDataSource(): Promise<DefaultOptionType[]> {
    const javaVersions = await getLibericaJavaVersions();
    const lts = [];
    const nonLts = [];
    for (const it of javaVersions) {
      const item = { label: it.featureVersion, value: it.featureVersion };
      it.LTS ? lts.push(item) : nonLts.push(item);
    }
    const list = [];
    if (lts.length > 0) list.push({ label: "LTS", options: lts });
    if (nonLts.length > 0) list.push({ label: "非LTS", options: nonLts });

    // 默认查询最新的LTS版本
    this.value = lts[0]?.value;
    return list;
  }
}

class LibericaBundleTypeCondition extends MirrorQueryCondition<string> {
  constructor() {
    super("包类型", "bundle-type");
  }

  async getDataSource(): Promise<DefaultOptionType[]> {
    const values = await getLibericaBundleTypes();
    return values.map(it => ({ label: getLibericaBundleTypeLabel(it), value: it }));
  }
}

/**
 * LibericaJDK下载源
 */
export class JdkLibericaMirror extends BaseEnvMirror {
  constructor() {
    super({
      id: "openjdk.liberica",
      name: "Liberica",
      url: `https://api.bell-sw.com/v1/liberica/releases/?os=${getLibericaOsName()}&arch=${getLibericaArch()}&bitness=${getBitness()}`,
      variant: "liberica",
      website: "https://bell-sw.com/",
    });
  }

  protected getConditions0(): MirrorQueryCondition[] {
    return [new LibericaJavaVersionCondition(), new LibericaBundleTypeCondition()];
  }

  async getDownloadList(query: Record<string, any>): Promise<ToolInstallVersionItem[]> {
    const api = this.getRequestUrl(query);
    try {
      const resp = await native.http.getJson<LibericaJdkVersionInfo[]>(api);
      if (__DEV__) console.log("getLibericaJdkDownloadList", resp.data);
      // 过滤出压缩包
      const list = resp.data.filter(it => it.installationType === "archive");
      list.sort((a, b) => b.updateVersion - a.updateVersion);

      return list.map(it => {
        // https://download.bell-sw.com/java/17.0.12+10/bellsoft-jdk17.0.12+10-windows-amd64.zip
        return {
          id: `liberica-${it.version}`,
          name: it.filename,
          version: it.version,
          // 将github地址转换为官网下载地址
          url: it.downloadUrl.startsWith("https://github.com") ? `https://download.bell-sw.com/java/${it.version}/${it.filename}` : it.downloadUrl,
          edition: "liberica",
        };
      });
    } catch (e: any) {
      console.error("getLibericaJdkDownloadList error", e);
      return [];
    }
  }
}
